package control;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;


public class ConnectionMysql {

	private String username			=	"root";
	private String userpassword 	= 	"12345";
	private String hostip			=	"localhost";
	private String hostport 		= 	"3306";
	private String name 			= 	"simuladoenem";
	
	private Connection conexao 		= 	null;
    private Statement st			=	null;
    private PreparedStatement ps	=	null;
    
public ConnectionMysql(){}
    
    public ConnectionMysql( String username,String userpassword,String hostip,String hostport,String name){
		
		this.username 		= username;
		this.userpassword 	= userpassword;
		this.hostip 		= hostip;
		this.hostport 		= hostport;
		this.name			= name;
		
	}

public Connection newConnection() throws Exception{//ClassNotFoundException {
        	     	    
		

		try{		
			
			Class.forName("com.mysql.jdbc.Driver");//.newInstance();	
			//Class.forName("com.mysql.jdbc.Driver");
    		 //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

        	 this.conexao = DriverManager.getConnection(
        			 	"jdbc:mysql://"+
        			 			this.hostip+":"+
        			 			this.hostport+"/"+
        			 			this.name, 
        			 			this.username, 
        			 			this.userpassword);
        	 
        	 //this.st	=	this.conexao.createStatement();
        	 
    	 }catch (ClassNotFoundException e) {
		   		   
    		 throw new Exception("Ocorreu um erro ao tentar localizar o drive para banco de dados.");		    
    	 
    	 }catch (SQLException e) {
		    
    		 throw new Exception("Ocorreu um erro ao tentar conectar com o banco de dados.");
    	 
    	 }catch (Exception e){
                 
    		 throw new Exception("Ocorreu um erro desconhecido: "+e.getMessage());
 
    	 }
    	 
    	 
    	 return conexao;
    	 
	}

	
	public Statement getStatement(){
		 
        return this.st;

    }

    public PreparedStatement getPreparedStatement(){

        return this.ps;

    }


}
